package com.yc.tomcat2.javax.servlet.http;

import com.yc.tomcat2.javax.servlet.YcServletResponse;

import java.io.*;

/**
 * 根据request请求信息获取响应
 * @author zwei
 * @create 2024-07-27 9:21
 */
public class YcHttpServletResponse implements YcServletResponse {
    private YcHttpServletRequest request;
    private OutputStream oos;
    public YcHttpServletResponse(YcHttpServletRequest req, OutputStream oos) {
        this.request = req;
        this.oos = oos;
    }
    @Override
    public void send() {
        String uri=this.request.getRequestURI();    //wowotuan/index.html
        String realpath=this.request.getRealPath(); //服务器路径
        File f=new File(realpath,uri);
        byte[] fileContent=null;
        String responseProtocol=null;
            if (!f.exists()){
            //文件不存在 响应404页面
            fileContent=readFile( new File(realpath+File.separator,"/404.html") );
            responseProtocol=gen404(fileContent);
        }else {
            //文件存在 读取文件
            fileContent=readFile(new File(realpath,uri));
            responseProtocol=gen200(fileContent);
        }
        try {
            oos.write(responseProtocol.getBytes());
            oos.flush();
            oos.write(fileContent);
            oos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (oos!=null){
                try {
                    this.oos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public OutputStream getOutputStream() {
        return oos;
    }

    @Override
    public PrintWriter getWriter() {
        return new PrintWriter(this.oos);
    }

    private String gen200(byte[] fileContent) {
        String protocol200="";
        //先取出请求的资源的类型
        String uri=this.request.getRequestURI();    //uri: /wowotuan/index.html
        //从uri取后缀名
        int index=uri.lastIndexOf(".");
        if (index>=0){
            index=index+1;
        }
        String fileExtension=uri.substring(index);
        String contentType;
        switch (fileExtension.toLowerCase()) {
            case "jpg":
                contentType = "image/jpeg";
                break;
            case "css":
                contentType = "text/css";
                break;
            case "js":
                contentType = "text/javascript";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "png":
                contentType = "image/png";
                break;
            default:
                contentType = "text/html;charset=utf-8";
        }
        protocol200 = "HTTP/1.1 200 OK\r\nContent-Type:" + contentType + "\r\nContent-Length:" + fileContent.length + "\r\n\r\n";
        return protocol200;
    }

    private String gen404(byte[] fileContent) {
        String protocol404="HTTP/1.1 404 Not Found\r\nContent-Type:text/html;charset=utf-8\r\nContent-Length:"+fileContent.length+"\r\n";
        protocol404+="Server:kitty server\r\n\r\n";
        return protocol404;
    }

    //读取本地文件
    private byte[] readFile(File file) {
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        try {
            FileInputStream fis=new FileInputStream(file);
            byte[] bs=new byte[1024];
            int len=-1;
            while ((len=fis.read(bs,0,bs.length))!=-1){
                bos.write(bs,0,len);
            }
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if (bos!=null){
                try {
                    bos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return bos.toByteArray();
    }
}
